package com.yanfan.zutai.controller;


import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.mqttclient.PubMqttClient;
import com.yanfan.mqtt.manager.MqttRemoteManager;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BMqttSubscribe;
import com.yanfan.zutai.service.IBMqttSubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * mqtt主题订阅Controller
 *
 * @author ruoyi
 * @date 2021-08-24
 */
@RestController
@RequestMapping("/synthesize/bMqttSubscribe")
public class BMqttSubscribeController extends BaseController {
    @Autowired
    private IBMqttSubscribeService bMqttSubscribeService;
    @Resource
    private MqttRemoteManager remoteManager;
    @Resource
    private PubMqttClient mqttClient;

    /**
     * 查询mqtt主题订阅列表
     */
    //@PreAuthorize("@ss.hasPermi('synthesize:bMqttSubscribe:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(BMqttSubscribe bMqttSubscribe) {
        startPage();
        List<BMqttSubscribe> list = bMqttSubscribeService.selectBMqttSubscribeList(bMqttSubscribe);
        return getDataTable(list);
    }

    /**
     * 导出mqtt主题订阅列表
     */
    //@PreAuthorize("@ss.hasPermi('synthesize:bMqttSubscribe:export')")
    @Log(title = "mqtt主题订阅", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @SysQueryData
    public AjaxResult export(BMqttSubscribe bMqttSubscribe) {
        List<BMqttSubscribe> list = bMqttSubscribeService.selectBMqttSubscribeList(bMqttSubscribe);
        ExcelUtil<BMqttSubscribe> util = new ExcelUtil<BMqttSubscribe>(BMqttSubscribe.class);
        return util.exportExcel(list, "mqtt主题订阅");
    }

    /**
     * 获取mqtt主题订阅详细信息
     */
    //@PreAuthorize("@ss.hasPermi('synthesize:bMqttSubscribe:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bMqttSubscribeService.selectBMqttSubscribeById(id));
    }

    /**
     * 新增mqtt主题订阅
     */
    //@PreAuthorize("@ss.hasPermi('synthesize:bMqttSubscribe:add')")
    @Log(title = "mqtt主题订阅", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BMqttSubscribe bMqttSubscribe) {
        mqttClient.subTopic(bMqttSubscribe.getTopic(), bMqttSubscribe.getQos());
        return toAjax(bMqttSubscribeService.insertBMqttSubscribe(bMqttSubscribe));
    }

    /**
     * 修改mqtt主题订阅
     */
    //@PreAuthorize("@ss.hasPermi('synthesize:bMqttSubscribe:edit')")
    @Log(title = "mqtt主题订阅", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BMqttSubscribe bMqttSubscribe) {
        return toAjax(bMqttSubscribeService.updateBMqttSubscribe(bMqttSubscribe));
    }

    /**
     * 删除mqtt主题订阅
     */
    //@PreAuthorize("@ss.hasPermi('synthesize:bMqttSubscribe:remove')")
    @Log(title = "mqtt主题订阅", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            BMqttSubscribe bMqttSubscribe = bMqttSubscribeService.selectBMqttSubscribeById(ids[i]);
            mqttClient.cleanTopic(bMqttSubscribe.getTopic());
        }
        return toAjax(bMqttSubscribeService.deleteBMqttSubscribeByIds(ids));
    }
}
